package top.swimmer.concurrent.thread;

import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class StressTest {
    public static void main(String[] args) {
        ExecutorService executors = Executors.newFixedThreadPool(3);
        CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
        Loader loaderA = new Loader(cyclicBarrier, "A");
        Loader loaderB = new Loader(cyclicBarrier, "B");
        Loader loaderC = new Loader(cyclicBarrier, "C");
        executors.execute(loaderA);
        executors.execute(loaderB);
        executors.execute(loaderC);
        executors.shutdown();
    }
}

class Loader implements Runnable {

    private CyclicBarrier cyclicBarrier;
    private String threadName;

    Loader(CyclicBarrier cyclicBarrier, String threadName) {
        this.cyclicBarrier = cyclicBarrier;
        this.threadName = threadName;
    }

    @Override
    public void run() {
        try {
            loadResources();
            cyclicBarrier.await();
        } catch (Exception e) {
            System.out.println(threadName + " load resources error.");
        }
        stressTesting();
    }

    private void loadResources() {
        System.out.println(threadName + " load resources.");
    }

    private void stressTesting() {
        System.out.println(threadName + " stress test begin.");
    }
}